package com.yeye.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yeye.model.User;
import com.yeye.service.UserService;
import com.yeye.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yeye1
 * @since 2023-06-01
 */
@RestController
@RequestMapping("/user")
public class UserController {

  @Autowired
  private UserService userService;

  @GetMapping("list")
  public R list() {
    return R.ok().put("data", userService.list());
  }
  @GetMapping("count")
  public R count() {
    return R.ok().put("data", userService.count());
  }

  @GetMapping("info/{id}")
  public R info(@PathVariable("id") Long id) {
    return R.ok().put("data", userService.getById(id));
  }

  @GetMapping("query/{name}")
  public R query(@PathVariable("name") String name) {
    return R.ok().put("data", userService.getOne(new LambdaQueryWrapper<User>().like(User::getName,name)));
  }

  @GetMapping("delete/{id}")
  public R delete(@PathVariable("id") Long id) {
    return R.ok().put("data", userService.removeById(id));
  }
  @PostMapping("save")
  public R save(@RequestParam User user) {
    return R.ok().put("data", userService.save(user));
  }

  @PostMapping("saveBatch")
  public R saveBatch(@RequestParam User user) {
    return R.ok().put("data", userService.saveBatch((Collection<User>) user));
  }
}
